package com.medvalley.tcm.controller.admin.controller;

import com.medvalley.tcm.base.Result;
import com.medvalley.tcm.controller.BaseController;
import com.medvalley.tcm.controller.admin.service.BackReportService;
import com.medvalley.tcm.controller.admin.vo.inputvo.ReportEditInputVo;
import com.medvalley.tcm.controller.admin.vo.inputvo.SearchReportInputVo;
import com.medvalley.tcm.controller.admin.vo.outputvo.PageDataOutputVo;
import com.medvalley.tcm.controller.admin.vo.outputvo.ReportOutputVo;
import com.medvalley.tcm.pojo.SickInfoBrief;
import com.medvalley.tcm.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController(value = "adminReportController")
@RequestMapping("/admin/report")
@Api(value = "PC端报告相关接口")
public class ReportController extends BaseController {

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    @Autowired
    private ReportService reportService;

    @Autowired
    private BackReportService backReportService;


    @ApiOperation(value = "报告列表-按时间筛选")
    @RequestMapping(value = "/list/date", method = RequestMethod.POST)
    public Result listReportByDate(@RequestParam(name = "begin", required = false) Date begin,
                                   @RequestParam(name = "end", required = false) Date end,
                                   @RequestParam(required = false) String orgName) {
        if (begin == null) {
            begin = new Date();
        }
        if (end == null) {
            end = new Date();
        }
        List<SickInfoBrief> SickInfoBrief = reportService.findReportListByDate(begin, end, orgName);
        return success(SickInfoBrief);
    }


    @ApiOperation(value = "报告检索")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result searchReport(@ModelAttribute SearchReportInputVo searchReportInputVo) {
        PageDataOutputVo<ReportOutputVo> pageDataOutputVo = backReportService.searchReport(searchReportInputVo);
        return success(pageDataOutputVo);
    }

    @ApiOperation(value = "删除报告")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result deleteReport(@RequestParam("reportId") long reportId) {
        backReportService.deleteReport(reportId);
        return success();
    }

    @ApiOperation(value = "删除报告")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result editReport(@ModelAttribute ReportEditInputVo reportEditInputVo) {
        backReportService.editReport(reportEditInputVo);

        return success();
    }

}
